﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace Youz.Common.Helper
{
    /// <summary>
    /// 谷歌验证类
    /// </summary>
    public class GoogleAuth
    {
        /// <summary>
        /// 获取秘钥
        /// </summary>
        /// <returns></returns>
        public static string GetKey()
        {
            string responseText = GetResponseText("http://googleauth.sinaapp.com/api/sn.html");
            GoogleAuthResult json = JsonConvert.DeserializeObject<GoogleAuthResult>(responseText);
            return json.Data.Sn;
        }

        /// <summary>
        /// 获取二维码
        /// </summary>
        /// <param name="key">秘钥</param>
        /// <param name="mark">标识符</param>
        /// <returns></returns>
        public static MemoryStream GetTwoDimensionCode(string key, string mark)
        {
            WebRequest request = WebRequest.Create(string.Format("http://googleauth.sinaapp.com/api/img.html?key={0}&mark={1}", key, mark));
            WebResponse response = request.GetResponse();
            MemoryStream ms = new MemoryStream();
            if (response != null)
            {
                System.Drawing.Image image = new System.Drawing.Bitmap(response.GetResponseStream());
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                image.Dispose();
            }
            return ms;
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="key">秘钥</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public static bool Verify(string key, string code)
        {
            string responseText = GetResponseText(string.Format("http://googleauth.sinaapp.com/api/verify.html?key={0}&code={1}", key, code));
            GoogleAuthResult json = JsonConvert.DeserializeObject<GoogleAuthResult>(responseText);
            if (json.Status == 1) return true;
            return false;
        }

        /// <summary>
        /// 发送http请求，获取响应字符串
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private static string GetResponseText(string url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            if (response != null)
            {
                Encoding responseEnc = Encoding.UTF8;
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return sr.ReadToEnd();
            }
            return string.Empty;
        }
    }

    /// <summary>
    /// 谷歌验证返回的数据实体
    /// </summary>
    public class GoogleAuthResult
    {
        public GoogleAuthData Data { get; set; }
        public string Info { get; set; }
        public int Status { get; set; }
    }

    /// <summary>
    /// 谷歌验证返回的数据实体中的秘钥
    /// </summary>
    public class GoogleAuthData
    {
        public string Sn { get; set; }
    }
}